package com.xish.api.operating;


import com.mysema.query.BooleanBuilder;
import com.xish.model.bean.Operating;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jdbc.query.QueryDslJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.xish.model.table.QOperating.operating1;

@Repository
public class OperatingRepository {

    @Autowired
    private QueryDslJdbcTemplate jdbcTemplate;

    public List<Operating> findByCondition(OperatingCondition condition) {
        if (condition == null) {
            return null;
        }

        BooleanBuilder where =
                new BooleanBuilder(operating1.sort.eq(condition.getSort()));
        where.and(operating1.status.eq(condition.getStatus()));
        where.and(operating1.startTime.loe(condition.getCurrentTime()));
        where.and(operating1.endTime.goe(condition.getCurrentTime()));

        if ((condition.getChannel() != null) && !condition.getChannel().isEmpty()) {
            where.and(operating1.channel.eq(condition.getChannel()).or(operating1.channel.isNull()));
        }

        //todo: platform and poi_id

        return jdbcTemplate.query(jdbcTemplate.newSqlQuery().from(operating1).where(where), operating1);
    }
}
